{include file="public/header"/}
{include file="public/left"/}
      
    <div class="content">
        <div class="crumbs clr">系统管理 <span class="red">> 角色管理</span>
            {if isset($list['id'])}
                <span class="red">> 编辑</span>
            {else /}
                <span class="red">> 新建</span>
            {/if}
        </div>
        <div class="whiteBox">
            {if isset($list['id'])}
                <p class="tittle">编辑角色</p>
            {else /}
                <p class="tittle">新建角色</p>
            {/if}
          <table class="addTab mb20">
           <tr>
              <td>上级角色：</td>
              <td>
                <select name="role" id="role">
                      <option value="0">无</option>
                      {volist name="role" id='item' key="key"}
                       <option {if isset($item['id']) && isset($list['id']) && $item['id'] eq $list['id'] }disabled{/if} {if isset($item['id']) && isset($list['pid'])  &&  $item['id'] eq $list['pid'] }selected{/if} value="{$item.id}">{$item.html|default=''}{$item.role_name}</option>
                      {/volist}
                </select>
              </td>
            </tr>
            <tr>
              <td>角色名称：</td>
              <td><input type="text" name="rolename" value="{$list.role_name|default=''}"></td>
            </tr>
            <tr>
              <td>选择菜单：</td>
              <td>
                <select name="menu" id="menu">
                      {volist name="menu" id='item' key="key"}
                      <option {if isset($item['pid']) &&  $item['pid'] eq 0 }disabled{/if} {if isset($item['id']) && isset($list['pid'])  &&  $item['id'] eq $list['pid'] }selected{/if}  value="{$item.id}">{$item.html|default=''}{$item.menu_name}</option>
                      {/volist}
                </select>
              </td>
            </tr>
            <tr>
              <td valign="top">选择权限：</td>
              <td>
                <span class="greyBtn chooseSign">选择权限</span>
                <ul class="signList clr mt30">
                  
                </ul>
              </td>
            </tr>
            <tr>
              <td valign="top">描　　述：</td>
              <td>
                <textarea class="loadArea" id="desc">{$list.desc|default=''}</textarea>
              </td>
            </tr>
            <tr>
              <td></td>
              <td align="right"><span class="greyBtn return">返回</span><span class="redBtn ml15 mr50 save">保存</span></td>
            </tr>
          </table>
        </div>
        
    </div>

{include file="public/footer"/}

<div class="alert">
  <p class="mask"></p>
  <div class="signMsg">
    <i></i>
    <h1>选择权限</h1>
    <div class="clr signBox pd15">
      <label class="checkAll" for="all"><input type="checkbox" id="all" name="all">全选</label>
      <ul class="signDl clr mt20">
        
      </ul>
    </div>
    
    <div class="alertBtm">
      <span class="cancel">取消</span>
      <span class="submit selFun">确认</span>
    </div>
  </div>
</div>
<script type="text/javascript">
var menuid = $('#menu option:selected').val();
var roleid = '{$list.id|default=''}';
getRoleMenuFun(menuid,roleid);
getMenuFun(menuid);
//返回
$('.return').on('click',function() {
  window.history.back();
});

$("#menu").change(function(){   
    var menuid = $('#menu option:selected').val();
    var roleid = '{$list.id|default=''}';
    if(menuid && roleid){
      getRoleMenuFun(menuid,roleid);
    }
});

$('.orgList').on('click','.delorg',function(){
  if (window.confirm('您确定要删除么？')) {
    $(this).parent().remove()
  }
});

$('.chooseSign').on('click',function() {
  var menuid = $('#menu option:selected').val();
  getMenuFun(menuid);
  $('.alert').show()
});

function getMenuFun(id){
  $(".signDl").html('');
  $.ajax({
    url : "{:url('admin/role/getMenuFun')}",
    type: "post",
    async: true,
    data: {'id':id},
      dataType:'json',
    success: function(data) {
      if(data.ret_data){
          $(".signDl").html(data.ret_data);
      }
    }
  });
}
function getRoleMenuFun(menuid,roleid){
  $(".signList").html('');
  $.ajax({
    url : "{:url('admin/role/getRoleMenuFun')}",
    type: "post",
    async: true,
      dataType:'json',
    data: {'menuid':menuid,'roleid':roleid},
    success: function(data) {
      if(data.ret_data){
          $(".signList").html(data.ret_data);
      }
    }
  });
}

$("#all").click(function(){   
    if(this.checked){   
        $(".signDl :checkbox").prop("checked", true);  
    }else{   
  $(".signDl :checkbox").prop("checked", false);
    }   
});
$('.signDl :checkbox').on('click',function(){
  $(this).each(function() {
    if(this.checked == false){
            $(".checkAll :checkbox").prop("checked", false);
        }
  });
});

$('.selFun').on('click',function(){
  var sign = [],
      ids = [],
      tags = [],
      list='',
      arrChk=$("input[name='sign']:checked");

      if (arrChk.length==0) {
          layer.alert('请选择权限');
        return false;
      } else {
        $(arrChk).each(function(){
          sign.push($(this).next().text());
          ids.push($(this).attr("signid"));
          tags.push($(this).attr("tag"));
        });
        for (var i=0;i<sign.length;i++)
        {
          list +="<li funid="+ids[i]+" funname="+sign[i]+" funtag="+tags[i]+"><i>x</i>"+sign[i]+"</li>";
        }
        $('.signList').html(list)
        $('.alert').hide()
      }
});

$('.cancel,.signMsg i').on('click',function(){
  $('.alert').hide()
});

$('.signList').on('click', 'i', function() {
    var t =this;
    var menuid = $('#menu option:selected').val();
    var roleid = '{$list.id|default=''}';
    var permissionsid=$(this).parent().attr("funid");
    layer.confirm('您确定要删除此权限么？', {
        btn: ['确认','取消'] //按钮
     }, function(index){
        delRoleMenuFun(menuid,roleid,permissionsid);
        $(t).parent().remove()
        layer.close(index);
     }, function(){
    });
});

function delRoleMenuFun(menuid,roleid,permissionsid){
  $.ajax({
    url : "{:url('admin/role/delRoleMenuFun')}",
    type: "post",
    async: true,
    data: {'menuid':menuid,'roleid':roleid,'permissionsid':permissionsid},
    success: function(data) {
       //layer.alert(data.msg);
    }
  });
}

$('.save').on('click',function(){
   var id='{$list.id|default=''}';
   var pid = $('#role option:selected').val();
   var rolename = $('input[name="rolename"]').val();
   if ($.trim(rolename)=='') {
        layer.alert('请输入角色名称');
        return false;
   }
   var menuid = $('#menu option:selected').val()
   if ($.trim(menuid)=='0') {
        layer.alert('请选择模块');
        return false;
   }
   var funnum=0;
   var funvals = "";
   var funtexts = "";
   var funtags = "";
   $(".signList li").each(function(){
      if(funnum==0){
        funvals+=$(this).attr("funid");
        funtexts+=$(this).attr("funname");
        funtags+=$(this).attr("funtag");
      }else{
        funvals+='#'+$(this).attr("funid");
        funtexts+='#'+$(this).attr("funname");
        funtags+='#'+$(this).attr("funtag");
      }
      funnum++;
    });
   var desc = $('#desc').val();
   $.post("{:url('admin/role/editAjax')}", {
      id : id,
      pid : pid,
      rolename : rolename,
      menuid : menuid,
      desc : desc,
      funnum:funnum,
      funvals:funvals,
      funtexts:funtexts,
      funtags:funtags,
    }, function(data){
      if(data.code==1){
        layer.alert(data.msg, function(index){
          location.href = "{:url('admin/role/index')}";
        });
      }else if(data.code==2){
        layer.alert(data.msg);
        return false;
      }else if(data.code==3){
        layer.alert(data.msg, function(index){
          location.href = "{:url('admin/role/index')}";
        });
      }else{
        layer.alert(data.msg);
        return false;
      }
    }, 'json');
});
</script>
</body>
</html>
    